/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "JoystickCtrlMainWindowMainFrame.h"


//*****************************************************************************
DEFINE_EVENT_TYPE(MY_EVENT_REFRESHTARGETSERVICES);
//*****************************************************************************
BEGIN_EVENT_TABLE(JoystickCtrlMainWindowMainFrame, wxFrame)
  EVT_COMMAND(wxID_ANY, MY_EVENT_REFRESHTARGETSERVICES, JoystickCtrlMainWindowMainFrame::OnRefreshTargetServices)
END_EVENT_TABLE();
//*****************************************************************************

JoystickCtrlMainWindowMainFrame::JoystickCtrlMainWindowMainFrame( wxWindow* parent )
  : MainFrame( parent )
{
}
//*****************************************************************************

bool JoystickCtrlMainWindowMainFrame::Initialize(gimi::GIMI *aGimiPtr, int aGimboID, int aReportID)
{
  bool result = true;
  
  // Store for future reference.
  iGIMIPtr = aGimiPtr;

  // Initialize the JoystickControl display
  iJoystickControl = new JoystickControl(aGimiPtr,
                                         this);
  GetSizer()->Add( iJoystickControl, 1, wxEXPAND | wxALL);
  this->Layout();

  // Start the Gimbo core.
  CComponent::Start(aGimiPtr, aGimboID, aReportID);

  // Unconditional LoadConfig.
  //  LoadConfig();

  return result;
}
//*****************************************************************************

void JoystickCtrlMainWindowMainFrame::Deinitialize()
{
  //  DisconnectServices();
  //CComponent::Stop(); \todo Unimplemented.
}
//*****************************************************************************

void JoystickCtrlMainWindowMainFrame::OnRefreshTargetServices(wxCommandEvent &aEvent)
{
  // Convert TServiceAssignmentList array to GIMIServiceArray.
  std::vector<gimi::GIMIService> gimiservicearray;
  for(EACH_IN_i(iServiceList)) {
    gimi::GIMIService gs = i->ToGIMIService();
    iGIMIPtr->findClientName(gs.clientName,
                             gs.clientId);
    dPrint(ODTEST,"Pushing to array service: '0x%08x', '%s', %d:%d",
           gs.clientId,
           gs.clientName.c_str(),
           gs.majorServiceId,
           gs.minorServiceId);
    gimiservicearray.push_back(gs);
  }
  
  // Assign services.
  dPrint(ODTEST,"Calling SetTargetServices() with %u entries", 
         gimiservicearray.size());
  assert(iJoystickControl != NULL);
  iJoystickControl->SetTargetServices(gimiservicearray);

  iServiceListUpdated = false;
}
//*****************************************************************************

void JoystickCtrlMainWindowMainFrame::OnServiceAssignment(const Gimbo::TServiceAssignmentList &aServiceList)
{
  dPrint(ODSTATUS,"Event: OnServiceAssignment");
  
  // Got service list - REPLACING current.
  iServiceList = aServiceList;
  iServiceListUpdated = true;

  // No add pending event.
  wxCommandEvent e( MY_EVENT_REFRESHTARGETSERVICES, GetId() );
  AddPendingEvent(e);
}
//*****************************************************************************

void JoystickCtrlMainWindowMainFrame::OnGimboDetach()
{
  // Detached from Gimbo - Oh dear - or... So?
  dPrint(1,"Event: OnGimboDetach");
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
